package model.patient;

import java.util.ArrayList;

import model.Queries;
import model.SQLiteDatabase;

public class PatientChildren extends SQLiteDatabase implements Queries{

	protected int patientIdpatient;
	protected int childrenIdchild;
	
	private ArrayList<PatientChildren> childrenList = new ArrayList<PatientChildren>();
	
	public PatientChildren() {
		this.setTableName("patient_children");
	}

	public ArrayList<PatientChildren> getChildrenList() {
		return childrenList;
	}	
	
	@Override
	public void select(String where) {
		
		this.connect();

		if (where.equals(null)) {
			this.setQuery("SELECT * FROM" + getTableName());
		} else {
			this.setQuery("SELECT * FROM" + getTableName() + " "+ where);
		} // if		
		
		try {			
			this.statement = this.connection.createStatement();
			this.resultSet = statement.executeQuery(this.getQuery());
			
			while (resultSet.next()) {
				PatientChildren child = new PatientChildren();
				
				child.patientIdpatient = resultSet.getInt("patient_idpatient");
				child.childrenIdchild = resultSet.getInt("children_idchild");
				
				childrenList.add(child);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}		
	}
	
	@Override
	public void insert() {
		this.connect();
		
		String insert;
		insert = "patient_idpatient="+this.patientIdpatient+", ";
		insert += "children_idchild="+this.childrenIdchild;
		
		this.setQuery("INSET INTO "+this.getTableName()+" SET "+insert);
		
		try {
			this.statement = this.connection.createStatement();
			this.resultSet = statement.executeQuery(this.getQuery());			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	@Override
	public void update(String where) {
		this.connect();

		String update;
		
		update = "patient_idpatient="+this.patientIdpatient+", ";
		update += "children_idchild="+this.childrenIdchild;
		
		this.setQuery("UPDATE "+this.getTableName()+" "+update+" WHERE "+where);

		try {
			this.statement = this.connection.createStatement();
			this.resultSet = statement.executeQuery(this.getQuery());			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}		
		
	}
	
	@Override
	public void delete() {
		this.connect();
		
		String where = "patient_idpatient="+this.patientIdpatient+" AND children_idchild="+this.childrenIdchild;
		
		this.setQuery("DELETE FROM"+this.getTableName()+" WHERE "+where);

		try {
			this.statement = this.connection.createStatement();
			this.resultSet = statement.executeQuery(this.getQuery());			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}	
	}
}
